//app.js

App({
  onLaunch: function () {
  },
  /**
   * 第一次关注小程序 点击允许授权登录
   */
  getUserInfo: function (encryptedData, iv, cb) {
    const that = this
    wx.login({
      success: function (res) {
        var code = res.code

        wx.request({
          url: that.globalData.website + 'webchat/login',
          data: {
            encryptedData: encryptedData,
            iv: iv,
            code: code
          },
          success: function (res) {
            
            if (res.data.message == '40001') {
              // 登录错误
              wx.hideLoading();
              wx.showModal({
                title: '提示',
                content: '无法登录，请重试',
                showCancel: false
              })
              return;
            } else {
              var userinfo = res.data.data.userInfo
              that.globalData.openid = userinfo.wxopenid
              
              wx.setStorageSync('uid', userinfo.wxopenid)

              typeof cb == "function" && cb(userinfo);
            }
          },
          fail: function (r) {
         
          }
        })
      }
    })  
  },

  /**
   * session_key未过期，直接从服务器获取用户信息
   */
  getUserInfoFromServer: function (uid, cb) {
    const that = this
    wx.request({
      url: that.globalData.website + 'webchat/info',
      data: {
        openid: uid
      },
      success: function (res) {
        
        if (res.data.message == "成功") {

          var userinfo = res.data.data;
          that.globalData.openid = userinfo.wxopenid
          typeof cb == "function" && cb(userinfo);
        }
      }
    })
  },

  /**
   * session_key 已过期 需要重新执行小程序登录 获取用户信息
   * 此时需要调用wx.getUserInfo 获取加密信息
   */
  getUserInfoLogin: function (cb) {

    var that = this;

    wx.login({
      success: function (r) {
        var code = r.code// 登录凭证

        wx.getUserInfo({
          success: function (res) {

            wx.request({
              url: that.globalData.website + 'webchat/login',

              data: {
                encryptedData: res.encryptedData,
                iv: res.iv,
                code: code
              },

              success: function (res) {

                if (res.data.message == '40001') {
                  // 登录错误
                  wx.hideLoading();
                  wx.showModal({
                    title: '提示',
                    content: '无法登录，请重试',
                    showCancel: false
                  })
                  return;
                } else {
                  var userinfo = res.data.data.userInfo
                  that.globalData.openid = userinfo.wxopenid
                  wx.setStorageSync('uid', userinfo.wxopenid)

                  typeof cb == "function" && cb(userinfo);
                }

              }
            });
          },

          fail: function (res) {
            /**
             * 用户点击了取消授权
             * 
             * 应该给予按钮重新授权
             */
            
            // wx.openSetting({
            //   success: (res) => {

            //     if (res.authSetting["scope.userInfo"]) {
            //       // 进入这里说明用户重新授权了，重新执行获取用户信息的方法  
            //       that.getUserInfo()
            //     }
            //   }
            // })
          }
        })
      },
      fail: function (ss) {

      }
    })

  },
  globalData: {
    website: 'https://immodian.cn/mini/',
    openid:""
  }
})